import { createPinia } from 'pinia'
import { createApp } from 'vue'
import './style.scss'
import MateChat from '@matechat/core'
import VueDevui from 'vue-devui'
import App from './App.vue'
import i18n from './i18n'
import router from '@/router'
import MyRouter from '@/router/myRouter'

const pinia = createPinia()
const app = createApp(App)

app.use(pinia)
app.use(MateChat)
app.use(VueDevui)
app.use(i18n)
// app.use(router)

// 初始挂载（可选）
// app.mount('#app-matechat')

window.vueMount = function (target = '#app-matechat') {
    // 如果已挂载则先卸载
    if (app._container) {
        app.unmount()
    }

    const container = document.querySelector(target)
    if (!container) {
        console.error(`Mount target "${target}" not found`)
        return null
    }

    container.innerHTML = '' // 清空容器
    app.mount(target)

    return {
        app,
        // Vue 路由
        // router,
        // 自定义路由
        router: MyRouter,
    }
}

window.vueUnMount = function (appInstance, target) {
    if (!appInstance || typeof appInstance.unmount !== 'function') {
        console.error('Invalid Vue app instance provided')
        return
    }

    // 卸载应用
    appInstance.unmount()

    // 清理目标容器
    if (target) {
        const container = document.querySelector(target)
        if (container) {
            container.innerHTML = ''
        }
    }
}